home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / s_to_z / tpack / restorer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  3.8 KB  |  158 lines

  1. unit Restorer;
  2.  
  3.  
  4. interface
  5.  
  6. uses Classes, Forms,
  7.   UserInfo
  8.   ,IniLink;
  9.  
  10. type
  11.  TRestorerFlag = (resLoad,resSave);
  12.  TRestorerFlags = set of TRestorerFlag;
  13.  
  14.  TGenericRestorer = class(TUserInfo)
  15.  private
  16.    fFlags: TRestorerFlags;
  17.    fFormClose: TCloseEvent;
  18.  protected
  19.    function GetActive: Boolean;
  20.    procedure SetActive(Value: Boolean); {will save when setting true in designmode}
  21.    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  22.  public
  23.    constructor Create(AOwner: TComponent); override;
  24.    destructor Destroy; override;
  25.    function UpdateOK:Boolean; Override;
  26.    procedure Save; virtual; abstract;
  27.    procedure Load; virtual; abstract;
  28.  published
  29.    property Active: Boolean read GetActive write SetActive stored false;
  30.    property Flags: TRestorerFlags read fFlags write fFlags default [resLoad,resSave];
  31.    end;
  32.  
  33.  TFormRestorer = class(TGenericRestorer)
  34.  private
  35.    fIniFileLink: TIniFileLink;
  36.  protected
  37.  public
  38.    procedure Notification(AComponent: TComponent; Operation: TOperation); Override;
  39.    procedure Save; Override;
  40.    procedure Load; Override;
  41.  published
  42.    property IniFileLink: TIniFileLink read fIniFileLink write fIniFileLink;
  43.    end;
  44.  
  45. implementation
  46.  
  47. constructor TGenericRestorer.Create(AOwner: TComponent);
  48. begin
  49.   inherited Create(AOwner);
  50.   Active:=True;
  51. end;
  52.  
  53. function TGenericRestorer.UpdateOK:Boolean;
  54. begin
  55. {  ComponentIndex:=0; }
  56.   Result:= inherited UpdateOK;
  57.   if Result and (resLoad in fFlags) then
  58.     Load;
  59.   if not cx.Designing then begin            {when initializing at runtime}
  60.     with TForm(Owner) do begin         {hook the save proc into the form's onclose handler}
  61.       fFormClose:=OnClose;
  62.       OnClose:=FormClose;
  63.       end;
  64.     end;
  65. end;
  66.  
  67. destructor TGenericRestorer.Destroy;
  68. {the action performed in here is normally not required as the form close proc has
  69. been called and we're on the way to destroying the form.. but aha! if you instantiate
  70. the component on the fly and remove it it would leave a hole in the close chain it you
  71. were not to clear the pointer here anyway. luckily the form first destroys its children,
  72. then itself, so the action here succeeds in either case.}
  73. begin
  74.   if assigned(fFormClose) then   {unhook}
  75.     with TForm(Owner) do begin
  76.       OnClose:=fFormClose;
  77.       fFormClose:=nil;
  78.       end;
  79.   inherited Destroy;
  80. end;
  81.  
  82.  
  83. function TGenericRestorer.GetActive: Boolean;
  84. begin
  85.   Result:=fFlags<>[];
  86. end;
  87.  
  88. procedure TGenericRestorer.SetActive(Value: Boolean);
  89. begin
  90.   if Value then begin
  91.     fFlags:=[resLoad,resSave];
  92.     if cx.Designing and (resSave in fFlags) then
  93.       Save;
  94.     end
  95.   else
  96.     fFlags:=[];
  97. end;
  98.  
  99. {}
  100.  
  101. procedure TGenericRestorer.FormClose(Sender: TObject;
  102.   var Action: TCloseAction);
  103. begin
  104.   if assigned(fFormClose) then
  105.     fFormClose(Sender,Action);
  106.   if (resSave in fFlags) then
  107.     Save;
  108. end;
  109.  
  110.  
  111. {}
  112.  
  113. procedure TFormRestorer.Notification(AComponent: TComponent; Operation: TOperation);
  114. begin
  115.   inherited Notification(AComponent, Operation);
  116.   if Operation = opRemove then begin
  117.     cx.NilIfSet(fIniFileLink,AComponent);
  118.     end;
  119. end;
  120.  
  121. procedure TFormRestorer.Save;
  122. var
  123.   wp: TWindowPosValue;
  124. begin
  125.   cx.MakeIfNil(fIniFileLink,TIniFileLink);
  126.   with tForm(Owner) do begin
  127.     wp.Top:=Top;
  128.     wp.Left:=Left;
  129.     wp.Height:=Height;
  130.     wp.Width:=Width;
  131.     end;
  132.   with fIniFileLink do begin
  133.     Section:='WindowPos';
  134.     WindowPosEntry[owner.classname]:=wp;
  135.     end;
  136. end;
  137.  
  138. procedure TFormRestorer.Load;
  139. var
  140.   wp: TWindowPosValue;
  141. begin
  142.   cx.MakeIfNil(fIniFileLink,TIniFileLink);
  143.   with fIniFileLink do begin
  144.     Section:='WindowPos';
  145.     wp:=WindowPosEntry[owner.classname];
  146.     end;
  147.   if wp.Width>0 then
  148.     with tForm(Owner) do begin
  149.       Top:=wp.Top;
  150.       Left:=wp.Left;
  151.       Height:=wp.Height;
  152.       Width:=wp.Width;
  153.       end;
  154. end;
  155.  
  156.  
  157. end.
  158.